﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Process.Entities;
using KZ.Exception;
using KZ.Utilities;
using System.Data;
using System.Data.SqlClient;
namespace Process.DAL
{
    public class CategoryDAL
    {
        private static CategoryENT CreateCategoryFromReader(IDataReader reader)
        {
            CategoryENT item = new CategoryENT();

            if (!reader.IsDBNull(reader.GetOrdinal(CategoryColumns.CategoryID.ToString()))) item.CategoryID = Utility.StringParse(reader[CategoryColumns.CategoryID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(CategoryColumns.CategoryName.ToString()))) item.CategoryName = Utility.StringParse(reader[CategoryColumns.CategoryName.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(CategoryColumns.ParentCategoryID.ToString()))) item.ParentCategoryID = Utility.StringParse(reader[CategoryColumns.ParentCategoryID.ToString()]);
            return item;
        }
        public static List<CategoryENT> ShowAllCategories()
        {
            try
            {
                List<CategoryENT> Categories = new List<CategoryENT>();
                using (IDataReader reader = DataAccessHelper.ExecuteQuery("[dbo].[GetListProductCategories]"))
                {
                    while (reader.Read())
                    {
                        Categories.Add(CreateCategoryFromReader(reader));

                    }
                    return Categories;

                }
            }
            catch (SqlException)
            {
                throw new DataAccessException("có lỗi xảy ra trong lúc thực thi truy vấn");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }


            
        }
    }
}
